{include file="common/head"/}
<script type="text/javascript" src="__STATIC__/common/js/jquery.2.1.1.min.js"></script>
<style>
    .layui-btn-xs{
    height: 22px;
    line-height: 22px;
    padding: 0 5px;
    font-size: 12px;}
    #tb-box{display: none;}
    .layui-title{overflow: auto;}
    .layui-elem-quote b{
        line-height:38px;
        font-size:18px;}
    .layui-elem-quote{
        height:38px;
    }
</style>
<div class="admin-main layui-anim layui-anim-upbit" ng-app="hd" ng-controller="ctrl">
    <div class="layui-tab layui-tab-brief" id="table-box">
        <ul class="layui-tab-title">
            <a href="{:url('index')}"><li>返回列表</li></a>
            <li onclick="fromAtion('edit','{$course_id}')">添加课程</li>
            <li class="layui-this">课程详情</li>
        </ul>
    </div>
    <!--<a href="{:url('addDateil',array('course_id'=>$course_id))}">添加</a>-->
    <blockquote class="layui-elem-quote layui-text">
        <b class="layui-text">{$course_name} </b>
        <button class="layui-btn layui-btn-normal" id="addDateil" style="float: right;">添加 <i class="fa fa-plus-circle"></i></button>
    </blockquote>
    <!--<div class="layui-title">
        <b class="layui-text">{$course_name} </b>
        <button class="layui-btn layui-btn-normal" id="addDateil" style="float: right;">添加 <i class="fa fa-plus-circle"></i></button>
    </div>-->

    <div class="layui-form">
        <table class="layui-table">
            <thead>
                <tr style="text-align: center;">
                    <th width="8%">章节ID</th>
                    <th>名称</th>
                    <th>时长(分)</th>
                    <th>类型</th>
                    <th width="8%">排序</th>
                    <th width="8%">状态</th>
                    <th width="8%">是否试听</th>
                    <th width="10%">操作</th>
                </tr>
            </thead>
            <tbody>
                {volist name = 'list' id='v'}
                    <tr>
                        <td>{$v.course_detail_id}</td>
                        <td>{$v.name}</td>
                        <td>{$v.times}</td>
                        <td>{$v.type|status=3}</td>
                        <td>
                            <div class="layui-table-cell laytable-cell-1-sort">
                                <input name="undefined" data-id="{$v['course_detail_id']}" class="list_order layui-input" value=" {$v['sort']}" size="10">
                            </div>
                        </td>
                        <td>
                            {if $v['status'] == 1}
                            <input type="checkbox" checked name="undefined"  value="{$v.course_detail_id}" lay-skin="switch" lay-filter="switchTest" title="开关">
                            {else/}
                            <input type="checkbox" name="undefined"  value="{$v.course_detail_id}" lay-skin="switch" lay-filter="switchTest" title="开关">
                            {/if}
                        </td>
                        <td>
                            {if $v['is_preview'] == 1}
                            <input type="checkbox" checked name="undefined"  value="{$v.course_detail_id}" lay-skin="switch" lay-filter="isPreview" title="开关">
                            {else/}
                            <input type="checkbox" name="undefined"  value="{$v.course_detail_id}" lay-skin="switch" lay-filter="isPreview" title="开关">
                            {/if}
                        </td>
                        <td>
                            <button data-id="{$v.course_detail_id}" class="layui-btn layui-btn-danger layui-btn-xs delDateil"><i class="fa fa-trash"></i></button>
                            <button data-id="{$v.course_detail_id}" class="layui-btn layui-btn-xs editDateil"><i class="fa fa-pencil"></i></button>
                            <button data-id="{$v.course_detail_id}" class="layui-btn layui-btn-normal layui-btn-xs addDateil"><i class="fa fa-plus-circle"></i></button>
                        </td>
                    </tr>

                    {notempty name="v.son"}
                        {volist name='v.son' id='v1'}
                            <tr>
                                <td>{$v1.course_detail_id}</td>
                                <td>{$nbsp} {$v1.name}</td>
                                <td>{$v1.times}</td>
                                <td>{$v1.type|status=3}</td>
                                <td>
                                    <div class="layui-table-cell laytable-cell-1-sort">
                                        <input name="undefined" data-id="{$v1['course_detail_id']}" class="list_order layui-input" value=" {$v1['sort']}" size="10">
                                    </div>
                                </td>
                                <td>
                                    {if $v1['status'] == 1}
                                    <input type="checkbox" checked name="undefined"  value="{$v1.course_detail_id}" lay-skin="switch" lay-filter="switchTest" title="开关">
                                    {else/}
                                    <input type="checkbox" name="undefined"  value="{$v1.course_detail_id}" lay-skin="switch" lay-filter="switchTest" title="开关">
                                    {/if}
                                </td>
                                <td>
                                    {if $v1['is_preview'] == 1}
                                    <input type="checkbox" checked name="undefined"  value="{$v1.course_detail_id}" lay-skin="switch" lay-filter="isPreview" title="开关">
                                    {else/}
                                    <input type="checkbox" name="undefined"  value="{$v1.course_detail_id}" lay-skin="switch" lay-filter="isPreview" title="开关">
                                    {/if}
                                </td>
                                <td>
                                    <button data-id="{$v1.course_detail_id}" class="layui-btn layui-btn-danger layui-btn-xs delDateil"><i class="fa fa-trash"></i></button>
                                    <button data-id="{$v1.course_detail_id}" class="layui-btn layui-btn-xs editDateil"><i class="fa fa-pencil"></i></button>
                                </td>
                            </tr>
                        {/volist}
                    {/notempty}
                {/volist}
            </tbody>
        </table>
    </div>
</div>
{include file="common/foot"/}
<script>

    layui.use(['form','table'], function(){
        var table = layui.table,$= layui.jquery,form = layui.form;

        $('body').on('blur','.list_order',function() {
            var course_detail_id = $(this).attr('data-id');
            var sort = $(this).val();
            var course_id = "{$course_id}";
            var loading = layer.load(1, {shade: [0.1, '#fff']});
            $.post('{:url("deitOrder")}',{course_id:course_id, course_detail_id:course_detail_id, sort:sort},function(res){
                layer.close(loading);
                if(res.code === 1){
                    layer.msg(res.msg, {time: 1000, icon: 1}, function () {
                        location.href = res.url;
                    });
                }else{
                    layer.msg(res.msg,{time:1000,icon:2});
                }
            })
        });

        //监听状态开关
        form.on('switch(switchTest)', function(){
            var status= this.checked ? 'true' : 'false';
            var course_detail_id = $(this).val();

            $.post('{:url("editStatus")}', {course_detail_id: course_detail_id,status:status}, function (data) {
                if (data.code === 1) {
                    layer.alert(data.msg, {icon: 6});
                }else{
                    layer.msg(data.msg,{icon:5});
                }
            });
        });



        //是否试听操作
        form.on('switch(isPreview)', function(){
            var is_preview= this.checked ? 'true' : 'false';
            var course_detail_id = $(this).val();

            $.post('{:url("isPreview")}', {course_detail_id: course_detail_id,is_preview:is_preview}, function (data) {
                if (data.code === 1) {
                    layer.alert(data.msg, {icon: 6});
                }else{
                    layer.msg(data.msg,{icon:5});
                }
            });
        });

    });

    function fromAtion(action,id){
        if(id == ''){
            layer.alert('课程ID丢失');
            return false;
        }else{

            var url = action+"?course_id="+id+"&long=0";
            location.href = "{:url('"+url+"')}";
        }
    }

    //重载方法
    function loades(url){
        window.location.href=url;
    }

    layui.use(['layer'], function () {

        //通过外层添加按钮添加
        $('#addDateil').click(function(){
            layer.open({
                type:2,
                title: '添加课时',
                shadeClose: true,
                shade: 0.8,
                maxmin: true, //开启最大化最小化按钮
                area: ['893px', '600px'],
                content: '{:url("addDateil",array('course_id'=>$course_id))}'
            });
        });

        //通过章节按钮添加
        $('.addDateil').click(function(){
            var pid = $(this).attr('data-id');

            layer.open({
                type:2,
                title: '添加课时',
                shadeClose: true,
                shade: 0.8,
                maxmin: true, //开启最大化最小化按钮
                area: ['893px', '600px'],
                content: '{:url("addDateil")}?pid='+pid+'&course_id={$course_id}'
            });
        });

        //修改操作
        $('.editDateil').click(function(){
            var course_detail_id = $(this).attr('data-id');
            layer.open({
                type:2,
                title: '添加课时',
                shadeClose: true,
                shade: 0.8,
                maxmin: true, //开启最大化最小化按钮
                area: ['893px', '600px'],
                content: '{:url("editDateil")}?course_detail_id='+course_detail_id+'&course_id={$course_id}'
            });
        });

        //删除操作
        $('.delDateil').click(function(){
            var course_detail_id = $(this).attr('data-id');
            var course_id = "{$course_id}";
            layer.confirm('您确定要删除该内容么?如果有子目录也会被删除的哦', {
                btn: ['确定','放弃'] //按钮
            }, function(){
                $.post(
                        "{:url('delDateil')}",
                        {course_detail_id:course_detail_id,course_id:course_id},
                        function(data){
                            if (data.code === 1) {
                                layer.alert(data.msg, {icon: 6}, function(index){
                                    layer.close(index);
                                    window.location.href=data.url;
                                });
                            }else{
                                layer.msg(data.msg,{icon:5});
                            }
                        }
                );
                layer.close(layer.index);
            }, function(){

            });
        });

    });


</script>